{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 用pytorch实现全连接神经网络"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 载入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import struct\n",
    "import torch as t\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 载入数据\n",
    "def load_mnist(path, kind):\n",
    "    \"\"\"Load MNIST data from `path`\"\"\"\n",
    "    labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind)\n",
    "    images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind)\n",
    "    with open(labels_path, 'rb') as lbpath:\n",
    "        magic, n = struct.unpack('>II', lbpath.read(8))\n",
    "        labels = np.fromfile(lbpath, dtype=np.uint8)\n",
    "\n",
    "    with open(images_path, 'rb') as imgpath:\n",
    "        magic, num, rows, cols = struct.unpack('>IIII', imgpath.read(16))\n",
    "        images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784)\n",
    "\n",
    "    return images, labels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "os.path.join()用于读取时拼接路径\n",
    "```python\n",
    "magic, n = struct.unpack('>II', lbpath.read(8))\n",
    "labels = np.fromfile(lbpath, dtype=np.uint8)\n",
    "```\n",
    "通过使用上面两行代码, 我们首先读入 magic number, 它是一个文件协议的描述, 也是在我们调用 fromfile 方法将字节读入 NumPy array 之前在文件缓冲中的 item 数(n). 作为参数值传入 struct.unpack 的 >II 有两个部分:\n",
    "- \\>: 这是指大端(用来定义字节是如何存储的); 如果你还不知道什么是大端和小端, Endianness 是一个非常好的解释. (关于大小端, 更多内容可见<<深入理解计算机系统 – 2.1 节信息存储>>)\n",
    "- I: 这是指一个无符号整数.\n",
    "(以上内容来自[liuchengxu_的CSDN博客](https://blog.csdn.net/simple_the_best/article/details/75267863 \"liuchengxu_的CSDN博客\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集: 60000 784\n",
      "测试集: 10000 784\n"
     ]
    }
   ],
   "source": [
    "x_train,y_train=load_mnist('./mnist','train')\n",
    "print('训练集:',x_train.shape[0],x_train.shape[1])\n",
    "x_test,y_test=load_mnist('./mnist','t10k')\n",
    "print('测试集:',x_test.shape[0],x_test.shape[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据集的格式表示共有60000张照片，60000行，每行784列，表示28*28*1=784个像素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 可视化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAagAAADRCAYAAACZ6CZ9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHIlJREFUeJzt3WmcVMXVx/HfiIKi4sJiTFxQRJYEBCGiBh8Ju0IggiBGRBaNAq6ASkjC7hbDpsiiICIY16jRxMQFRCNRUJSIG6AGFRUUQVREFJ3nBZ9Tt3q6Z+iZ6aW6+/99w03dOz3lze2pW1WnThUVFxcjIiISmj2yXQEREZFE1ECJiEiQ1ECJiEiQ1ECJiEiQ1ECJiEiQ1ECJiEiQ1ECJiEiQ1ECJiEiQ1ECJiEiQ9izPxbVq1SquW7dumqqSO9atW8emTZuKKvs5up+76H6m3ooVKzYVFxfXrsxn6H5GUnE/QffUJPudL1cDVbduXV566aWK1ypPtGzZMiWfo/u5i+5n6hUVFb1X2c/Q/Yyk4n6C7qlJ9juvIT4REQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQlSuRbqhuaDDz4AYNq0aa5sypQpAFxxxRUAXHbZZe7c4YcfnsHaiYhIZagHJSIiQcq5HtSHH37ojps3bw7A559/7sqKinald5o6dSoA8+fPd+c+/fTTTFQxL912220AXHTRRa7shx9+AGD16tWu7Nhjj81sxQK2Y8cOAL777jtX9txzzwHRc3zeeee5c3vumXNfx5TYtGmTO965cycAy5cvB6B79+7u3B57lO99esCAAQDMnj3blVWpUqXC9RR48803AWjfvr0rW7lyJQC1a1c6VWEc9aBERCRIOfPK9t57u3I1tmnTxpVt2bIFiHpNAAcccAAA1apVA+CTTz5x5959910AjjzySFemN6qyLVq0CIBhw4YBid9i/ftfqKwXP2nSJFe2ePFiAJYtW1bqz/kjAqNHj05T7cKyYcMGAO68804Abr31VnfOeuXvv/8+EPu8lfc5u+OOOwA46KCDXNnEiROB6O9DqNauXQtEf+NOOOGEbFbHsWe5Xbt2Gfl96kGJiEiQ1ECJiEiQghzi8yeVbWivc+fOQBRaXppmzZoBcM011wDQunVrd65+/fpA7JDCoEGDUlDj/LVmzRoAvvnmmyzXJBx+sI0tcbB/t2/f7s4VFxcDcNRRR7mymjVrArBixQogdgJ/8ODBQHomm0MycuRIABYuXJiR32dLTyAK8qlXr15GfndF2dD6W2+9BWR3iM+eY4iGHu3vQrqpByUiIkEKsgd15ZVXuuPp06eX62efeeYZALZt2wbAGWec4c49+OCDALzyyiuVrWJee+ONN9zx2LFjY84df/zx7viJJ54AYN99981IvbLFeo82wT5z5kx3buvWraX+XJMmTYDomYQojPqQQw4BYOPGjXGfle89qF/96ldA4h7Uj3/8YwBGjBgBREETkDhA59///jcADz30UMrrmU033XQTAB07dsxyTeCrr75yx9dddx0QmwAhnc+relAiIhIkNVAiIhKkoIb4LADC7/r7E3QQO2TXs2dPAPr27evKLN9eo0aNALj66qvduQceeCDhZ8oub7/9NgCnn366K9u8eXPMNddff707tjVn+W7p0qVA7H97aRo3buyOn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It3G2Zgr8UYevWrUDiLbmNLWuwZxgqvn2G5UcreRy6tWvXAjB27FhXZpvebd682ZUlM8RnQ6bLly93ZfbdTpTb0PLYaXuY1LHpGAsEgsxvqqkelIiIBClrYeb+RKr1oCzH1sEHH+zO2SRrIpY5unPnzq7s4osvTmk9c4Ufam8Lc+0N3l8oagsblbE8WrhtOccgyhu5dOnSuOv79u0LQIcOHVyZPYO25Xg+bzq4O/379wcS5x70gzsSBY2UZEEq/sZ7ibZ8txBoW7ybzbxx+cbud1kjMelWuN8mEREJmhooEREJUkaG+Pw1EO3btwfgqaeeirvOAif84SpTp04dAAYPHuzKKrp+Kh/5E8cl758NZYG21PAtWrQIiDZ7g2hoz98e4qyzzgKiCfh831AwHSZMmFDpz7AtevwsNOPGjQPyZ1uSEC1evNgd2/YymaIelIiIBCkjrx3+pKhN4NsWzVB2SPjEiROBKNdZzZo101FFKUAWPGJbuZc8lvKxkHJ/k9DJkyeX6zMaN24MRH8z/M0M7W+A37uV9PEzAGWLelAiIhKkjA/cWq4zf5FkogWTUj4/+clP3HGXLl2A2HxyIulm+1Nde+21ruz//u//gNi9nyy7vu0z1K1bN3fOerCp3CNOysdyQfo5J7NFPSgREQmSGigREQmSYjPzhD8k8vDDD2exJlLo/JDvrl27ArHZOiRsFkqezQwSRj0oEREJkhooEREJkhooEREJkhooEREJkhooEREJkhooEREJUlF58i0VFRV9Cry32wvz35HFxcW1K/shup+O7mfqVfqe6n7G0DOaWkndz3I1UCIiIpmiIT4REQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQmSGigREQnSnuW5uFatWsV169ZNU1Vyx7p169i0aVNRZT9H93MX3c/UW7Fixabi4uLalfkM3c9IKu4n6J6aZL/z5Wqg6taty0svvVTxWuWJli1bpuRzdD930f1MvaKiovcq+xm6n5FU3E/QPTXJfuc1xCciIkFSAyUiIkEq1xCfFIZNmza541/84hcA7Ny5E4B33nknK3USkcKjHpSIiARJPShxxo0bB8CsWbNc2aeffgpAv379slInESlc6kGJiEiQ1ECJiEiQNMRXoLZt2wZAr169XNnjjz8OQFFRtH6uVatWANxyyy0ZrJ2IiHpQIiISqOB7UD/88AMAO3bsKPWa+fPnu2PrGbzxxhsATJ061Z0bNWoUANOnT3dl++yzDwCTJk0CYPDgwamodrAshHzEiBEAPPHEE3HXzJs3zx3//Oc/B6L7JBKyb7/91h137twZiF0a8d///heAAw88MLMVkwpRD0pERIKUtR7U1q1b3fH3338PRG83/lv9559/DsCtt95ars+3hIzDhw93ZXPnzgXggAMOcGWnnHIKAG3bti3X5+eqL774AoCFCxeWeo2fzLJhw4bprpJIUr788suYf3377rsvACtWrHBlS5YsAeC4445zZRoJyC3qQYmISJDUQImISJAyPsS3fv16AJo1a+bKtmzZkrLP32OPXW2uDef5XfpBgwYBUKdOHVe23377AVC7dqW3egmWn1vvtNNOA6C4uDjuumXLlgGp2/6i0P3lL38B4JtvvnFlq1atAuCmm26Ku7558+YABbcdw8cff+yO7b6sW7cu7jobvkuUD9KCnOz+QvSM169f35VZ0FUhsXt5xx13APCvf/3LnXvxxRfjrr/rrrsAOPzwwwF48skn3bn+/fsDsdMA6aQelIiIBCnjPaiaNWsCcMghh7iy8vSgOnbsGPdZDz74oCurVq0aAG3atKlMNfPK3Xff7Y7t7bNv375AbMj9/vvvn9mK5YE1a9YA0bIGW+wMMGfOHCBxb9VfDG1effVVAI4//nhX9vLLL6eusoFaunSpO/7Tn/5U6nV77703AJdddpkrs+++Hwxl7B4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Tp04HYnJzZoh6UiIgESQ2UiIgEKS1DfMOHD4/5VyTXLFy40B3fddddQJTb8JJLLnHn/Al+iWdD9P369XNl/rFkXuvWrd1x7969AbjvvvtcmWXxmDZtGpDdwDT1oEREJEhBZTMXCUWXLl3c8ciRIwFYsGABoF6T5LZq1aq5YwteadCggSuzZRVjx44FshssoR6UiIgESQ2UiIgESUN8Igk0atTIHe/cuTOLNRFJHxvus4woJY+zTT0oEREJUpG/pfJuLy4q+hTYfTK9/HdkcXFxpWfKdT8d3c/Uq/Q91f2MoWc0tZK6n+VqoERERDJFQ3wiIhIkNVAiIhIkNVAiIhIkNVAiIhIkNVAiIhIkNVAiIhIkNVAiIhIkNVAiIhIkNVAiIhKk/we/c92hMZ4D8wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 10 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "fig, ax = plt.subplots(\n",
    "    nrows=2,\n",
    "    ncols=5,\n",
    "    sharex=True,\n",
    "    sharey=True, )\n",
    "\n",
    "ax = ax.flatten()\n",
    "for i in range(10):\n",
    "    img = x_train[y_train == i][0].reshape(28, 28)\n",
    "    ax[i].imshow(img, cmap='Greys', interpolation='nearest')   # Greys为白底黑字，gray为黑底白字\n",
    "#interpolation 插值，作nearest插值运算\n",
    "\n",
    "ax[0].set_xticks([]) #表示不设置刻度\n",
    "ax[0].set_yticks([])\n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面这一部分调用matplotlib进行可视化处理\n",
    "plt.subplots() 返回一个 Figure实例fig 和一个 AxesSubplot实例ax ，fig代表整个图像，<font color=red>ax代表坐标轴和画的图</font>\n",
    "plt.subplots(2,5)之后返回的ax为2*5的数组，所以用ax.flatten()进行折叠处理，得到一维数组，然后将x_train中的像素写入ax\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 网络\n",
    "从反向逆推导入手，来看一看pytorch如何实现全连接层的构建\n",
    "首先知道我们的输入是60000个784维的数据，则每个神经元就有784个权值，再加上1个偏置项  y=Wx+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "from torch.autograd import Variable as v"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
